package bairui.international.service.impl;

import javax.persistence.LockModeType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.common.config.Config_Path;
import bairui.international.common.tool.Util_GenerateQRCode;
import bairui.international.model.domain.jpa.TB_BranchCompany;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_User;

/**
 * 
 * 【(分公司员工)TB_BranchCompanyStaff】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_BranchCompanyStaff 模型
 * @see Service_BranchCompanyStaff 服务接口
 * @see BService_BranchCompanyStaffImpl 服务实现基础
 *
 */
@Service
public class Service_BranchCompanyStaffImpl extends BService_BranchCompanyStaffImpl implements Service_BranchCompanyStaff {

	@Autowired private Config_Path config_Path;
	@Autowired private Service_User service_User;
	
	@Override
	public void cancelDirector(long id) throws Exception {
		TB_BranchCompanyStaff bean = findById(id);
		if(!bean.getNexts().isEmpty()){
			throw new ValidateObjectException("object", "", "下级必须为空");
		}
		bean.setDirectorFlag(false);
	}

	/**
	 * 通过名称查找（加锁）
	 */
	@Override
	public TB_BranchCompanyStaff findByIdUseLock(Long id){
		return dao
				.find(QRoot.id.eq(id))
				.setLockMode(LockModeType.PESSIMISTIC_READ)
				.oneResult();
	}
	
	@Override
	public void upDirector(long id) throws Exception {
		TB_BranchCompanyStaff bean = findById(id);
		if(null != bean.getPrev()){
			throw new ValidateObjectException("object", "", "不能存在上级");
		}
		bean.setDirectorFlag(true);
	}

	@Override
	public TB_BranchCompanyStaff create(TB_BranchCompany branchCompany, TB_User user) throws Exception {
		user = service_User.load(user);
		// 创建对象
		TB_BranchCompanyStaff bean = createInstance(branchCompany, user);
		// 创建二维码
		String fileName = Util_GenerateQRCode.generateQRCode(
				config_Path.getPosition_Image_QR_CompanyStaff(), 
				"companyStaff", 
				String.valueOf(bean.getId()),
				user.getAvatarUrl()
			);
		bean.setDimensionalUrl(fileName);
		//
		return bean;
	}

	private TB_BranchCompanyStaff createInstance(TB_BranchCompany branchCompany, TB_User user) {
		return dao.save(new TB_BranchCompanyStaff(
				new TB_BranchCompanyStaff.MustProperties() {
					@Override public TB_User getUser() {
						return user;
					}
					@Override public Boolean getDirectorFlag() {
						return false;
					}
					@Override public TB_BranchCompany getBranchCompany() {
						return branchCompany;
					}
					@Override public Boolean getActiveFlag() {
						return true;
					}
					@Override public Double getMoney() {
						return 0d;
					}
					@Override public Double getWithdrawMoney() {
						return 0d;
					}
				},
				new TB_BranchCompanyStaff.OptionalSingleProperties() {
					@Override public TB_BranchCompanyStaff getPrev() {
						return null;
					}
					@Override public String getDimensionalUrl() {
						return null;
					}
				}
			));
	}

	@Override
	public void addNext(long id, TB_BranchCompanyStaff next) throws Exception {
		next = load(next);
		if(null != next.getPrev()){
			throw new ValidateObjectException("object", "", "已经存在上级");
		}
//		TB_BranchCompanyStaff prev = load(new TB_BranchCompanyStaff(id));
		if(next.getDirectorFlag()){
			throw new ValidateObjectException("object", "", "负责人不能被添加");
		}
		next.setPrev(new TB_BranchCompanyStaff(id));
	}

	@Override
	public void removePrev(long id, TB_BranchCompanyStaff prev) throws Exception {
		TB_BranchCompanyStaff bean = findById(id);
		bean.setPrev(null);
	}
	
	@Override
	public TB_BranchCompanyStaff getActive(TB_User user){
		return findOne(it->it.user.eq(user).and(it.activeFlag.isTrue()));
	}

	@Override
	public void resetDimensionalUrl(TB_BranchCompanyStaff bean, String fileName) {
		load(bean).setDimensionalUrl(fileName);
	}

}
